//
//  ChangePassViewController.m
//  Runone
//
//  Created by runone01 on 16/9/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ChangePassViewController.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "UserPass.h"
#import "ParModel.h"
#import "changePassModel.h"
#import "PromptUtil.h"
#import "UIView+Message.h"
#import "LoginViewController.h"
#import "CommonUtilityjudge.h"
#import "Define.h"
#import "AppLocalManager.h"

@interface ChangePassViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *OldPassWord;
@property (weak, nonatomic) IBOutlet UITextField *NewPassWord;
@property (weak, nonatomic) IBOutlet UITextField *confirmPassWord;
@property(nonatomic,strong)UserPass *userPass;
@property(nonatomic,strong)ParModel *parModel;
@property(nonatomic,strong)changePassModel *changeModel;
@property(nonatomic,strong)UIView *messageView;
@property(nonatomic,strong)PromptUtil *Message;
@property(nonatomic,strong)LoginViewController *logVC;
@property(nonatomic,strong)NSString *state;
@end

@implementation ChangePassViewController

#pragma   ---------   修改密码


- (void)viewDidLoad {
    [super viewDidLoad];
    _logVC=[[LoginViewController alloc] init];
    _userPass=[[UserPass alloc] init];
    _parModel=[[ParModel alloc] init];
    _OldPassWord.secureTextEntry=YES;
    _confirmPassWord.secureTextEntry=YES;
    _NewPassWord.secureTextEntry=YES;
    _Message=[[PromptUtil alloc] init];
    self.preferredContentSize=CGSizeMake(310, 186 );
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)Click:(UIButton *)sender {
    NSString *str=GetRememberPassWord;
    
    if (stringIsEmpty(_confirmPassWord.text)||stringIsEmpty(_NewPassWord.text)) {
        [_Message alert:@"请输入新密码!~"];
        return;
    }
    if (stringIsEmpty(_OldPassWord.text)) {
        [_Message alert:@"请输入旧密码!~"];
        return;
    }
    if (stringIsEmpty(_confirmPassWord.text)) {
        [_Message alert:@"请输入确认密码!"];
        return;
    }
    if (![_OldPassWord.text isEqualToString:str]) {
        [_Message alert:@"旧密码错误!"];
        return;
    }
    if (_confirmPassWord.text.length>16) {
        [_Message alert:@"密码长度不能超过16个字符"];
        return;
    }
    if (![_confirmPassWord.text isEqualToString:_NewPassWord.text]) {
        [_Message  alert:@"新密码和确认密码不一致！"];
        return;
    }
    if ([_confirmPassWord.text isEqualToString:str]) {
        [_Message  alert:@"新密码不能和旧密码相同！"];
        return;
    }
    if ([_confirmPassWord.text isEqualToString:_NewPassWord.text]) {
        [self request];
    }
    else
    {
        [_Message alert:@"两次密码不一致，请重新输入！"];
        return;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//刷新
-(void)request
{
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCurrentDutyPersonalName;
    getCurrentEventModel.DataValue = [self jsonstring];
    getCurrentEventModel.UpdatedTime=@"2012-01-12 12:00:00";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"UpdateUserPassword";
    
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            _changeModel=[changePassModel mj_objectWithKeyValues:requestOutginseng.DataValue];
            _state=_changeModel.State;
            if (self.changePassBlock!=nil) {
                self.changePassBlock(_changeModel.State);
            }
            if ([_state isEqualToString:@"1"]) {
                // [self dismissViewControllerAnimated:YES completion:nil];
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"\n修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
                
            }
            else
            {
                
                return;
            }
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"\n请求修改失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }

    };
    
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        [_confirmPassWord resignFirstResponder];
        [self presentViewController:_logVC animated:YES completion:nil];
    }
}

-(NSString *)jsonstring
{
    _userPass.key=@"OldPassword";
    _parModel.key=@"NewPassword";
    
    _userPass.value=self.OldPassWord.text;
    _parModel.value=self.NewPassWord.text;
    NSMutableDictionary *OldDic=_userPass.mj_keyValues;
    NSMutableDictionary *NewDic=_parModel.mj_keyValues;
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    [arr addObject:OldDic];
    [arr addObject:NewDic];
    NSString *dateValue=[arr mj_JSONString];
    return dateValue;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
